package com.future.lock.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.lock.R;
import com.future.lock.common.MyApp;
import com.future.lock.common.RefreshDataUtils;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.home.adapter.PeopleManageAdapter;
import com.future.lock.home.entity.bean.LockUser;
import com.future.lock.home.entity.response.LockUsersResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PeopleManageActivity extends BaseActivity {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private PeopleManageAdapter adapter;
    private String lock_id;
    private String lock_name;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_people_manage);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        setTitle("成员管理");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initAdapter();
        recyclerView.setAdapter(adapter);
    }

    private void initAdapter() {
        //初始化
        adapter = new PeopleManageAdapter(R.layout.item_people_manage, null);
        //头视图
        View headerView = getLayoutInflater().inflate(R.layout.layout_header_people_manage, null);
        ((TextView) headerView).setText("以下为"+lock_name+"的成员信息");
        adapter.addHeaderView(headerView);
        //空视图
        View emptyView = getLayoutInflater().inflate(R.layout.layout_default_empty, null);
        ((TextView) emptyView.findViewById(R.id.tv_tib)).setText("暂无成员");
        adapter.setEmptyView(emptyView);
        //点击事件
        adapter.setOnItemClickListener((adapter, view, position) -> {
            LockUser lockUser = (LockUser) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putString("un_user_id",lockUser.un_user_id);
            bundle.putString("remark",lockUser.remark);
            startActivityForResult(EditPeopleNickActivity.class,bundle,0x321);
        });

        getData();
    }

    private void getData() {
        HttpUtils.getHttpApi()
                .getLockUsers(MyApp.getUserToken(), lock_id)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        LockUsersResponse lockUsersResponse = new LockUsersResponse();
                        lockUsersResponse.parse(response.body());
                        if (lockUsersResponse.success == 1) {
                            adapter.setNewData(lockUsersResponse.lockUsers);
                        } else {
                            if (lockUsersResponse.code == 112) {
                                RefreshDataUtils.logout(PeopleManageActivity.this);
                            }
                            toast(lockUsersResponse.msg);
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        toast(t.getMessage());
                    }
                });
    }

    @Override
    protected void initLogic() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        lock_id = extras.getString("lock_id", "");
        lock_name = extras.getString("lock_name", "");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == resultCode && requestCode == 0x321) {
            getData();
        }
    }
}
